<template>
  <div class="warpper_containner">
    <div class="from_login">
      <div class="title">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form ref="form" :model="form">
        <el-form-item>
          <span>
            <img src="@/assets/user.png" alt="" />
          </span>
          <el-input
            v-model="form.user_name"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <span>
            <img :src="isPwd? require('@/assets/pwd_close.png'): require('@/assets/pwd_open.png')" alt="" @click="isPwd = !isPwd" />
          </span>
          <el-input
            :type="isPwd ? 'password' : 'text'"
            v-model="form.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="error"
            plain
            size="medium"
            :loading="loading"
            @click="handleClick"
            @keyup.enter="handleClick"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: { user_name: "", password: "" }, // 账号和密码的集合
      loading: false, // 显示loading
      isPwd: true, // 是否为密码
      user_id: "", // 用户id
    };
  },
  methods: {
    // 点击登录按钮
    async handleClick() {
      this.loading = true;
      Object.values(this.form).forEach((item) => {
        if (!item) {
          this.$message.error("请输入用户名称和密码");
        }
      });
      let res = await this.$post("/users/login", this.form);
      if (res.code == 0) {
        this.$message.success(res.msg);
        this.user_id = res.user_id;
        this.$store.commit("setToken", res.token);
        this.$router.push("/");
        this.getUserInfo();
      }
      this.loading = false;
    },
    // 获取用户信息
    async getUserInfo() {
      let res = await this.$get("/info/Details", { user_id: this.user_id });
      if (res.code == 0) {
        this.$store.commit("setUserInfo", res.result);
      }
    },
  },
};
</script>

<style scoped lang="scss">
.warpper_containner {
  .title {
    height: 150px;
    text-align: center;
    margin-bottom: 10px;
    img {
      height: 100%;
      border-radius: 50%;
    }
  }
  position: relative;
  width: 100%;
  height: 100%;
  background: url("../assets/bg.jpg");
  .from_login {
    padding: 20px 30px;
    position: absolute;
    left: 50%;
    top: 50%;
    min-width: 500px;
    transform: translate(-50%, -50%);
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 20px;
    /deep/ .el-form-item__content {
      position: relative;
      margin-top: 20px;
      height: 50px;
      width: 100%;
      text-align: center;
      .el-input {
        height: 100%;
        .el-input__inner:focus {
          border: #fff solid 1px;
        }
        input {
          border: none;
          padding-left: 60px;
          height: 100%;
          border-radius: 40px;
          background-color: rgba(0, 0, 0, 0.1);
          outline: none;
          border: 1px solid #fff;
          color: #fff;

          font-size: 18px;
          &::placeholder {
            font-size: 14px;
            color: #ccc;
          }
        }
      }
      & > span {
        position: absolute;
        left: 20px;
        top: 10px;
        height: 68%;
        width: 34px;
        z-index: 99;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .el-button {
        width: 100%;
        height: 100%;
        text-align: center;
        border-radius: 40px;
        font-size: 20px;
        color: #fff;
        background-color: rgb(233, 81, 107);
        border: none;
      }
    }
  }
}
</style>
